package com.onetech.adrewards.free.fragments;

import jp.co.CAReward_Common.CARCheck;
import jp.co.CAReward_Media.CARMApiCall;
import jp.co.CAReward_Media.CARMWebView;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import com.onetech.adrewards.free.R;


public class GetPointsFragment2 extends BaseFragment {
	public static final String LOGTAG = "CARMIntent->";
	private LinearLayout linearLayout=null;
	
	private CARMWebView mCARMWebView;
	private String get_url;
	private String get_m_id;
	private String get_m_owner_id;
	private String get_user_id;
	private String get_platform_id;
	private String get_api_key;
	private String get_app_key;
	private String get_loading_msg;
	private String get_timeout;
	private static CARMApiCall apiCall = null;
	private	ProgressDialog dialog = null;
	private Activity activity=null;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (linearLayout == null) {
			linearLayout = (LinearLayout) inflater.inflate(R.layout.get_points_fragment_layout,
					container, false);	
			
			initView();
			//initListener();
		} else {
			((ViewGroup) linearLayout.getParent()).removeView(mView);
		}
		return linearLayout;
	}
	private void initView(){
		activity=getActivity();
		mCARMWebView = (CARMWebView)linearLayout.findViewById(R.id.myAdRewardWebView);
		
		Intent intent = new Intent();
		intent.putExtra("m_id", "2835");
		intent.putExtra("m_owner_id", "558");
		intent.putExtra("user_id", "CAR20130118001");
		intent.putExtra("platform_id", "0");
		intent.putExtra("url", "http://car.mobadme.jp/spg/spnew/558/2835/index.php");
		intent.putExtra("api_key", "76552d4faa4dd35b");
		intent.putExtra("app_key", "ncIdX3la");
		intent.putExtra("loading_msg", "loading..");
		intent.putExtra("timeout", "3000");
		

		intent.putExtra("show_button", true);
		intent.putExtra("button_string", "back");
		intent.putExtra("bk_color", Color.BLUE);
		intent.putExtra("button_gravity", Gravity.RIGHT);

		String btnValue = null;
		int bkColor = -1;
		int gravity = -1;
		boolean cancelable = false;
		
		

		// Root化チェック
		CARCheck check = new CARCheck(activity);
		if(check.isSuperUserCustomized()) {
			Log.d(LOGTAG,"super user customized");
			return;
		}
		
		// パラメータ取得
		this.setInitPrm(intent);
		cancelable = intent.getBooleanExtra("cancelable", false);
		
		/* ボタン表示用 */
		boolean showButton = intent.getBooleanExtra("show_button", false);
		
		if(showButton) {
			btnValue = intent.getStringExtra("button_string");
			bkColor = intent.getIntExtra("bk_color", Color.TRANSPARENT);
			gravity = intent.getIntExtra("button_gravity", Gravity.LEFT);
			if (btnValue == null || "".equals(btnValue)) {
				btnValue = "戻る";
			}
			linearLayout.addView(this.makeButtonArea(btnValue, bkColor, gravity));
		}
		
		// AndroidプラットフォームIDを設定する
		get_platform_id = "3";

		// サーバタイムアウト時間を設定する(10秒)
		get_timeout = "10000";
		
		// パラメータチェック
		if(!this.chkInput()) {
			return;
		}

		// Debug Log
		this.showLog();

		if(apiCall != null) {
			apiCall.cancel(true);
		}
		dialog = new ProgressDialog(activity);
		dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		dialog.setMessage(get_loading_msg);
		dialog.setCancelable(cancelable);
		dialog.show();
		apiCall = new CARMApiCall(intent, activity, linearLayout, this.mCARMWebView, this.dialog);
		apiCall.execute();
	}
	
	private LinearLayout makeButtonArea(String btnValue, int bkColor, int gravity) {
		
		LinearLayout buttonArea = new LinearLayout(activity);
		Button button = new Button(activity);
		LayoutParams buttonParams = new LayoutParams(LayoutParams.WRAP_CONTENT, 
				LayoutParams.WRAP_CONTENT);
		
		buttonArea.setGravity(gravity);
		buttonArea.setBackgroundColor(bkColor);
		buttonArea.setOrientation(LinearLayout.HORIZONTAL);
		button.setText(btnValue);
		button.setLayoutParams(buttonParams);
		button.setOnClickListener(this);
		buttonArea.addView(button);
		
		return buttonArea;
	}
	
	public void setWebView(CARMWebView mCARMWebView) {
		
		this.mCARMWebView = mCARMWebView;
	}
	
	@Override
	public void onPause() {
		super.onPause();
		try{
			this.dialog.dismiss();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	@Override
	public void onResume() {
		super.onResume();
		if(this.mCARMWebView != null) {
			mCARMWebView.reload();
		} else {
			activity.finish();
		}
	}
	@Override
	public void onDestroy() {
		if(mCARMWebView != null) mCARMWebView.destroy();
		super.onDestroy();
	}
	
	
	private void showLog() {
		
		Log.d(LOGTAG, "url:" + get_url);
		Log.d(LOGTAG, "m_id:" + get_m_id);
		Log.d(LOGTAG, "m_owner_id:" + get_m_owner_id);
		Log.d(LOGTAG, "user_id:" + get_user_id);
		Log.d(LOGTAG, "api_key:" + get_api_key);
		Log.d(LOGTAG, "app_key:" + get_app_key);
		Log.d(LOGTAG, "loading_msg:" + get_loading_msg);
	}
	
	private void setInitPrm(Intent intent) {
		
		get_url = intent.getStringExtra("url");
		get_m_id = intent.getStringExtra("m_id");
		get_m_owner_id = intent.getStringExtra("m_owner_id");
		get_user_id = intent.getStringExtra("user_id");
		get_platform_id = intent.getStringExtra("platform_id");
		get_api_key = intent.getStringExtra("api_key");
		get_app_key = intent.getStringExtra("app_key");
		get_loading_msg = intent.getStringExtra("loading_msg");
		get_timeout = intent.getStringExtra("timeout");
	}
	
	private boolean chkInput() {
		
		if(get_url.length() <= 0) {
			Log.d(LOGTAG, "url is null");
			return false;
		}
		if(get_m_id.length() <= 0) {
			Log.d(LOGTAG, "m_id is null");
			return false;
		}
		if(get_m_owner_id.length() <= 0) {
			Log.d(LOGTAG, "m_owner_id is null");
			return false;
		}
		if(get_user_id.length() <= 0) {
			Log.d(LOGTAG, "user_id is null");
			return false;
		}
		if(get_platform_id.length() <= 0) {
			Log.d(LOGTAG, "platform_id is null");
			return false;
		}
		if(get_api_key.length() <= 0) {
			Log.d(LOGTAG, "api_key is null");
			return false;
		}
		if(get_app_key.length() <= 0) {
			Log.d(LOGTAG, "app_key is null");
			return false;
		}
		if(get_timeout.length() <= 0) {
			Log.d(LOGTAG, "timeout is null");
			return false;
		}
		
		try {
			Integer.parseInt(get_timeout);
		} catch(Exception e) {
			Log.d(LOGTAG, "timeout is invalid");
			return false;
		}
		
		return true;
	}

	public void onClick(View v) {
			activity.finish();
	}
}
